package com.taru.order.controller;


import com.taru.order.common.Data;
import com.taru.order.common.JsonResult;
import com.taru.order.entity.Note;
import com.taru.order.entity.Problem;
import com.taru.order.service.ProblemServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ProblemController {

    ProblemServiceImpl problemService=new ProblemServiceImpl();

    /**
     * 添加问题
     */
    @RequestMapping(value = "/problems",method = RequestMethod.POST)
    @ResponseBody
    public JsonResult addProblem(@RequestBody Problem problem){
        JsonResult result=null;
        try{
            int code=problemService.addProblem(problem);
            if(code>0){
                result =new JsonResult(200,"添加成功");
            }else {
                result=new JsonResult(404,"添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 获取最新的问题（列表）
     */
    @RequestMapping(value = "/problems",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listProblems(Integer pageNum,Integer pageSize){
        JsonResult result=null;
        try{
            Data data=problemService.listProblem(pageNum, pageSize);
            if(data!=null){
                result =new JsonResult(200,"获取成功",data);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 修改问题
     */
    @RequestMapping(value = "/problems",method = RequestMethod.PUT)
    @ResponseBody
    public JsonResult modProblem(@RequestBody Problem problem){
        JsonResult result=null;
        try{
            int code=problemService.modProblem(problem);
            if(code>0){
                result =new JsonResult(200,"修改成功");
            }else {
                result=new JsonResult(404,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 删除问题
     */
    @RequestMapping(value = "/problems",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResult removeProblems(@RequestBody List<Integer> list){
        JsonResult result=null;
        try{
            int code=problemService.deleteProblems(list);
            if(code>0){
                result =new JsonResult(200,"删除成功");
            }else {
                result=new JsonResult(404,"删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
    /**
     * 获取问题详情（根据问题id，附带答案）
     */
    @RequestMapping(value = "/problems/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult listProblem(@PathVariable("id") String id){
        JsonResult result=null;
        try{
            Problem problem=problemService.getProblem(id);
            if(problem!=null){
                result =new JsonResult(200,"获取成功",problem);
            }else {
                result=new JsonResult(404,"获取失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult(500,"服务器内部错误");
        }
        return result;
    }
}
